package energy;

import java.util.List;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Event;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;

import graph.*;

public class Graph extends JPanel {

	private static Logger log = Logger.getLogger("energy.Graph");
	
	G2Dint graph;
	DataSet data;
	DataSet state;
	Axis    xaxis;
    Axis    energyaxis;
    Axis 	stateaxis;
    boolean init;
    
	public Graph() {
        graph = new G2Dint();
        graph.drawzero = false;
        graph.drawgrid = false;
        graph.borderTop = 50;
        graph.borderRight = 40;
        
        try {
			graph.setMarkers(new Markers((new File("resources/marker.txt")).toURI().toURL()));
		} catch (MalformedURLException e) {
			log.error("Wrong marker file.",e);
		} catch (IOException e) {
			log.error("Error reading marker file.", e);
		}
        
		init = false;
		
        //graph.startedloading();
        //test();
        
		setLayout( new BorderLayout() );
		add(new JLabel("Energy Bucket by Morten Hansen and Jacob Andersen",0), BorderLayout.CENTER);
		//add(new Test(), BorderLayout.CENTER);
	}

	
	private void init() {
        //xaxis = graph.createAxis(Axis.BOTTOM);
		xaxis = graph.createXAxis();
        xaxis.setTitleText("Time (ms)");
        xaxis.setTitleColor( new Color(0,0,255) );
        //xaxis.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
        //xaxis.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        
        //yaxis = graph.createAxis(Axis.LEFT);
        energyaxis = graph.createYAxis();
        energyaxis.setTitleText("Current (A)");
        energyaxis.setTitleColor( new Color(0,255,0) );
        
        stateaxis = graph.createAxis(Axis.RIGHT);
        stateaxis.setTitleText("State");
        stateaxis.setTitleColor( new Color(255,0,0) );
        
        add(graph, BorderLayout.CENTER);
        
        //createListeners();
        
        init = true;
	}
	
	private void createListeners() {
		graph.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				graph.mouseDown(new Event(null,0,null), e.getX(), e.getY());
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}

			public void mousePressed(MouseEvent e) {
				graph.mouseDown(new Event(null,0,null), e.getX(), e.getY());
			}
			public void mouseReleased(MouseEvent e) {
				graph.mouseUp(new Event(null,0,null), e.getX(), e.getY());
			}
		});
		graph.addMouseMotionListener(new MouseMotionListener() {
			public void mouseDragged(MouseEvent e) {
				graph.mouseDrag(new Event(null,0,null), e.getX(), e.getY());
			}
			public void mouseMoved(MouseEvent e) {
				graph.mouseMove(new Event(null,0,null), e.getX(), e.getY());
			}			
		});
		graph.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				graph.keyDown(new Event(null,0,null), e.getKeyCode());
				log.debug("pressed: " + e.getKeyCode());
			}
			public void keyReleased(KeyEvent e) {}
			public void keyTyped(KeyEvent e) {}
		});
	}
	
	public void setData(List<EnergyData> dataList) {
		if(!init) init();
		
		double[] dataArray = new double[2*dataList.size()];
		double[] stateArray = new double[2*dataList.size()];
		int i = 0;
		for(EnergyData e : dataList) {
			dataArray[i] = e.time / 32.768;
			dataArray[i+1] = e.current;
			stateArray[i] = e.time / 32.768;
			stateArray[i+1] = e.state;
			i += 2;
		}
		
		graph.detachDataSets();
		xaxis.detachAll();
        energyaxis.detachAll();
        stateaxis.detachAll();
        
		data = graph.loadDataSet(dataArray, dataList.size());
        data.linestyle = 1;
        data.marker    = 1;
        data.markerscale = 1.5;
        data.markercolor = new Color(0,255,0);
        data.legend(200,100,"Current");
        data.legendColor(Color.black);
        
        state = graph.loadDataSet(stateArray, dataList.size());
        state.linestyle = 0;
        state.marker    = 1;//9
        state.markerscale = 1.5;
        state.markercolor = new Color(255,0,0);
        state.legend(200,120,"State");
        state.legendColor(Color.black);
        
        xaxis.attachDataSet(data);
        xaxis.attachDataSet(state);
        energyaxis.attachDataSet(data);
        stateaxis.attachDataSet(state);
        
        //log.debug("redraw");
        
        graph.repaint();
        
		updateUI();
	}
	
	private void test() {
		int np = 100;
		double[] d = new double[2*np];
		int i,j;
		for(i=j=0; i<np; i++,j+=2) {
			d[j] = j-np;
	        d[j+1] = 60000 * Math.pow( ((double)d[j]/(np-2) ), 2);
	    }

		data = graph.loadDataSet(d,np);
		data.linestyle = 0;
		data.marker    = 1;
		data.markerscale = 1.5;
		data.markercolor = new Color(0,0,255);
		data.legend(200,100,"nej nej");
		data.legendColor(Color.black);

		xaxis.attachDataSet(data);
		energyaxis.attachDataSet(data);
	
	}	
	
}
